package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 异步操作数据库
 *
 * @author Fxz
 * @version 1.0
 * @date 2022-02-16 18:16
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CartAsyncServiceImpl implements CartAsyncService {

	private final CartInfoMapper cartInfoMapper;

	/**
	 * 新增
	 */
	@Async
	@Override
	public void saveCartInfo(CartInfo cartInfo) {
		log.info("异步新增购物车数据:{}", cartInfo);
		cartInfoMapper.insert(cartInfo);
	}

	/**
	 * 更新
	 */
	@Async
	@Override
	public void updateCartInfo(CartInfo cartInfo) {
		cartInfoMapper.update(cartInfo, Wrappers.<CartInfo>lambdaUpdate().eq(CartInfo::getSkuId, cartInfo.getSkuId())
				.eq(CartInfo::getUserId, cartInfo.getUserId()));
	}

	/**
	 * 删除
	 */
	@Async
	@Override
	public void deleteCartInfo(String userId) {
		cartInfoMapper.delete(Wrappers.<CartInfo>lambdaQuery().eq(CartInfo::getUserId, userId));
	}

	/**
	 * 更改选中状态
	 */
	@Async
	@Override
	public void checkCart(String userId, Integer isChecked, Long skuId) {
		cartInfoMapper.update(new CartInfo().setUserId(userId).setSkuId(skuId).setIsChecked(isChecked),
				Wrappers.<CartInfo>lambdaUpdate().eq(CartInfo::getUserId, userId).eq(CartInfo::getSkuId, skuId));
	}

	/**
	 * 删除
	 */
	@Async
	@Override
	public void deleteCartInfo(String userId, Long skuId) {
		cartInfoMapper
				.delete(Wrappers.<CartInfo>lambdaQuery().eq(CartInfo::getSkuId, skuId).eq(CartInfo::getUserId, userId));
	}

}
